﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Components;
using System.Reflection;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.ComponentModel;
using Base;

namespace Core.Component
{
	public class SGVControlHelper
    {
        #region Constant

        public const String strTop = "Top";
        public const String strLeft = "Left";
        public const String strWidth = "Width";
        public const String strHeight = "Height";
        public const String strBackColor = "BackColor";
        public const String strForeColor = "ForeColor";
        public const String strType= "Type";
        public const String strName = "Name";
        public const String strTag = "Tag";
        public const String strText = "Text";
        public const String strFontName = "FontName";
        public const String strFontSize = "FontSize";
        public const String strFontStyle = "FontStyle";

        #endregion

        public static Dictionary<String, Type> dicMapingControls = new Dictionary<string, Type>();
        public static void Init()
        {
            dicMapingControls.Add("SGVToolbar", typeof(Core.Component.WebForm.SGVToolbar));
            dicMapingControls.Add("SGVPanel", typeof(Core.Component.WebForm.SGVPanel));
            dicMapingControls.Add("SGVLabel", typeof(Core.Component.WebForm.SGVLabel));
            dicMapingControls.Add("SGVTextBox", typeof(Core.Component.WebForm.SGVTextBox));
            dicMapingControls.Add("SGVNumericTextBox", typeof(Core.Component.WebForm.SGVTextBox));
            dicMapingControls.Add("SGVComboBox", typeof(Core.Component.WebForm.SGVComboBox));
            dicMapingControls.Add("SGVCheckBox", typeof(Core.Component.WebForm.SGVCheckBox));
            dicMapingControls.Add("SGVDateTimePicker", typeof(Core.Component.WebForm.SGVDateTimePicker));
            dicMapingControls.Add("SGVButton", typeof(Core.Component.WebForm.SGVButton));
            dicMapingControls.Add("SGVScreen", typeof(Core.Component.WebForm.SGVScreen));
            dicMapingControls.Add("SGVMainScreen", typeof(Core.Component.WebForm.SGVScreen));
            dicMapingControls.Add("TabControl", typeof(Core.Component.WebForm.SGVTabControl));
            dicMapingControls.Add("SGVTabControl", typeof(Core.Component.WebForm.SGVTabControl));
            dicMapingControls.Add("TabPage", typeof(Core.Component.WebForm.SGVTabPanel));
            dicMapingControls.Add("SGVGridView", typeof(Core.Component.WebForm.SGVGridView));
            dicMapingControls.Add("SGVLookUp", typeof(Core.Component.WebForm.SGVComboBox));
            dicMapingControls.Add("SGVTextBoxColumn", typeof(Core.Component.WebForm.SGVBoundField));
        }

        public static void ControlLostFocus(Control control)
        {
            control.BackColor = Color.White;
            Form frm = control.FindForm();
            if (frm != null && frm is SGVScreen)
            {
                ((SGVScreen)frm).SGVShortcutString = String.Empty;
            }
        }

        public static void ControlFocus(Control control)
        {
            control.BackColor = Color.Yellow;
            if (control is ISGVControl)
            {
                Form frm = control.FindForm();
                if (frm != null && frm is SGVScreen)
                {
                    ((SGVScreen)frm).SGVShortcutString = ((ISGVControl)control).SGVShortcutString;
                }
            }
        }

        public static String GetControlKeyName(String strScreenName, string strControlName)
        {
            String strKey = String.Format(@"{0}|{1}", strScreenName, strControlName);

            return strKey;
        }


        //public static WebControl CreateWebControl(UIComponent component)
        //{
        //    //WebControl control = 
        //}

        public static Dictionary<String, String> GetSerializableProperties(Control control)
        {
            Dictionary<String, String> dic = new Dictionary<string, String>();
           
            dic.Add(strName, control.Name);
            if(control.Tag != null)
                dic.Add(strTag, control.Tag.ToString());

            dic.Add(strTop, control.Location.Y.ToString());
            dic.Add(strLeft, control.Location.X.ToString());
            dic.Add(strWidth, control.Size.Width.ToString());
            dic.Add(strHeight, control.Size.Height.ToString());
            dic.Add(strBackColor, control.BackColor.ToArgb().ToString());
            dic.Add(strForeColor, control.ForeColor.ToArgb().ToString());
            dic.Add(strFontName, control.Font.Name);
            dic.Add(strFontSize, control.Font.Size.ToString());
            dic.Add(strFontStyle, control.Font.Style.ToString());
            dic.Add(strText, control.Text);
            
            return dic;
        }

        public static void CreateUIComponentFromControl(UIComponent component, Control control)
        {
            String type = String.Empty;
            if (control is SGVMainScreen)
                type = "SGVMainScreen";
            else if (control is SGVScreen)
                type = "SGVScreen";
            else
                type = control.GetType().Name;

            component.strType = type;

            CopyPropertyControl(control, component);
            if (control is ISGVControl)
                CopyPropertyISGVControl((ISGVControl)control, component);

            if (control is IBindingControl)
                CopyPropertyIBindingControl((IBindingControl)control, component);
        }
        public static System.Web.UI.Control CreateWebControlFromUIComponent(UIComponent component)
        {
            System.Web.UI.Control webControl = null;

            if (dicMapingControls.ContainsKey(component.strType) == false)
                return webControl;

            webControl = (System.Web.UI.Control)Activator.CreateInstance(dicMapingControls[component.strType]);
            if(webControl == null) return webControl;

            if (webControl is ISGVControl)
                ((ISGVControl)webControl).Deserialize(component);

            //foreach (UIComponent childComponent in component.lstChilds)
            //{
            //    WebControl childControl = CreateWebControlFromUIComponent(childComponent, false);
            //    if (childControl != null)
            //    {
            //        if (webControl is TabContainer)
            //            ((TabContainer)webControl).Tabs.Add(childControl);
            //        else
            //            webControl.Controls.Add(childControl);
            //    }
            //}

            return webControl;
        }

        public static DataControlField CreateDataControldFieldFromUIComponent(UIComponent component)
        {
            DataControlField webControl = null;

            if (dicMapingControls.ContainsKey(component.strType) == false)
                return webControl;

            webControl = (DataControlField)Activator.CreateInstance(dicMapingControls[component.strType]);
            if (webControl == null) return webControl;

            if (webControl is ISGVControl)
                ((ISGVControl)webControl).Deserialize(component);

            return webControl;
        }

        public static void PopulateProperties(WebControl webControl, UIComponent component)
        {
            webControl.ID = component.Name;
            //webControl.Width = component.Size.Width;
            //webControl.Height = component.Size.Height - 2;
            webControl.BackColor = component.BackColor;
            webControl.Font.Name = component.FontName;
            webControl.Font.Size = new System.Web.UI.WebControls.FontUnit(component.FontSize);
            webControl.ForeColor = component.ForeColor;
            webControl.CssClass = component.strType;
            webControl.Attributes["style"] = GenerateCssString(component);
        }

        public static String GenerateCssString(UIComponent component)
        {
            StringBuilder strBuilder = new StringBuilder();

            strBuilder.Append("position:absolute;float:left;");
            strBuilder.Append(String.Format(@"width:{0}px;", component.Size.Width));
            strBuilder.Append(String.Format(@"height:{0}px;", component.Size.Height));
            strBuilder.Append(String.Format(@"top:{0}px;", component.Location.Y));
            strBuilder.Append(String.Format(@"left:{0}px;", component.Location.X));
            //if (component.Dock == DockStyle.None || component.Dock == DockStyle.Left || component.Dock == DockStyle.Right)
                strBuilder.Append(String.Format(@"width:{0}px;", component.Size.Width));
            //else
               // strBuilder.Append(String.Format(@"width:100%;"));
            strBuilder.Append(String.Format(@"height:{0}px;", component.Size.Height));
            strBuilder.Append(String.Format(@"font-family:{0};", component.FontName));
            strBuilder.Append(String.Format(@"font-size:{0}px;", component.FontSize + 3));
            if(component.FontStyle == FontStyle.Bold)
                strBuilder.Append(String.Format(@"font-weight:bold;"));
            if(component.FontStyle == FontStyle.Italic)
                strBuilder.Append(String.Format(@"font-style:italic;"));

            return strBuilder.ToString();
        }

        public static void CopyPropertyIBindingControl(IBindingControl source, IBindingControl dest)
        {
            dest.BindingColumn = source.BindingColumn;
            dest.BindingPropertyName = source.BindingPropertyName;
            dest.BindingTable = source.BindingTable;

            dest.DataSourceColumns = source.DataSourceColumns;
            dest.DataSourceDisplayColumn = source.DataSourceDisplayColumn;
            dest.DataSourceKeyColumn = source.DataSourceKeyColumn;
            dest.DataSourceTable = source.DataSourceTable;
        }

        public static void CopyPropertyISGVControl(ISGVControl source, ISGVControl dest)
        {
            dest.SGVTag = source.SGVTag;
            dest.SGVShortcutString = source.SGVShortcutString;
        }

        public static void CopyPropertyControl(Control source, UIComponent dest)
        {
            dest.Name = source.Name;
            dest.Location = source.Location;
            dest.Size = source.Size;
            dest.FontName = source.Font.Name;
            dest.FontStyle = source.Font.Style;
            dest.FontSize = source.Font.Size;
            dest.BackColor = source.BackColor;
            dest.ForeColor = source.ForeColor;
            dest.Dock = source.Dock;
        }
	}

    public static class FontXmlConverter
    {
        public static string ConvertToString(Font font)
        {
            try
            {
                if (font != null)
                {
                    Style _style = new System.Web.UI.WebControls.Style();
                    FontInfo fontInfo = _style.Font;
                    fontInfo.Name = font.Name;
                    fontInfo.Size = new FontUnit(font.Size);
                    fontInfo.Bold = font.Bold;
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(FontInfo));
                    return converter.ConvertToString(fontInfo);
                }
                else
                    return null;
            }
            catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); }
            return null;
        }
        public static FontInfo ConvertToFont(string fontString)
        {
            try
            {
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(FontInfo));
                return (FontInfo)converter.ConvertFromString(fontString);
            }
            catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); }
            return null;
        }
    }
}
